home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C05 Animation / P01 CopyBits B&W / CopyBitsB&W.c next >
Encoding:
C/C++ Source or Header  |  1995-08-05  |  4.1 KB  |  157 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    CopyBitsB&W.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #define      rDisplayWindow        128
  13. #define      rBackPicture          128
  14. #define      rForePicture          129
  15.  
  16.  
  17. //____________________________________________________________
  18.  
  19. void     InitializeToolbox( void );
  20. GrafPtr  MakeNewBitMapAndSetPort( Rect * );
  21.  
  22.  
  23. //____________________________________________________________
  24.  
  25. void    main( void )
  26. {
  27.    WindowPtr  theWindow;
  28.    GrafPtr    theBackGrafPtr;
  29.    GrafPtr    theForeGrafPtr;
  30.    GrafPtr    theMergeGrafPtr;
  31.    PicHandle  theBackPicture;
  32.    PicHandle  theForePicture;
  33.    Rect       theRect;
  34.    short      theWidth;
  35.    short      theHeight;
  36.    short      left = 135;
  37.    short      top  = 50;
  38.    short      count = 0;
  39.  
  40.    InitializeToolbox();
  41.  
  42.    HideCursor();
  43.    
  44.    theBackPicture = GetPicture( rBackPicture );
  45.    if ( theBackPicture == nil )
  46.       ExitToShell();    
  47.       
  48.    theRect = (**theBackPicture).picFrame;    
  49.    OffsetRect( &theRect, - theRect.left, - theRect.top );
  50.  
  51.    theWidth = theRect.right - theRect.left;
  52.    theHeight = theRect.bottom - theRect.top;
  53.  
  54.    theWindow = GetNewWindow( rDisplayWindow, nil, (WindowPtr)-1L );
  55.    SizeWindow( theWindow, theWidth, theHeight, true );    
  56.    ShowWindow( theWindow );
  57.    
  58.    theBackGrafPtr = MakeNewBitMapAndSetPort( &theRect );        // 1st bitmap
  59.    DrawPicture( theBackPicture, &theRect );
  60.     
  61.    theMergeGrafPtr = MakeNewBitMapAndSetPort( &theRect );       // 2nd bitmap
  62.     
  63.    theForePicture = GetPicture( rForePicture );
  64.    if ( theBackPicture == nil )
  65.       ExitToShell();    
  66.  
  67.    theRect = (**theForePicture).picFrame;
  68.  
  69.    theWidth = theRect.right - theRect.left;
  70.    theHeight = theRect.bottom - theRect.top;
  71.    SetRect( &theRect, left, top, left + theWidth, top + theHeight );
  72.     
  73.    theForeGrafPtr = MakeNewBitMapAndSetPort( &theRect );        // 3rd bitmap
  74.    DrawPicture( theForePicture, &theRect );
  75.  
  76.    while ( !Button() )
  77.    {
  78.       ++count;
  79.  
  80.       if ( count <= 50 )
  81.          ++top;
  82.       else if ( ( count > 50 ) && ( count <= 100 ) )
  83.          --top;
  84.       else 
  85.       {
  86.          count = 1;
  87.          ++top;
  88.       }
  89.  
  90.       CopyBits( &(theBackGrafPtr->portBits), &(theMergeGrafPtr->portBits),
  91.                 &(theBackGrafPtr->portBits.bounds), &(theMergeGrafPtr->portBits.bounds),
  92.                 srcCopy, nil );
  93.         
  94.       theRect = theForeGrafPtr->portBits.bounds;
  95.  
  96.       SetRect( &theRect, left, top, left + theWidth, top + theHeight );
  97.  
  98.       CopyBits( &(theForeGrafPtr->portBits), &(theMergeGrafPtr->portBits),
  99.                 &(theForeGrafPtr->portBits.bounds), &theRect,
  100.                 srcOr, nil );
  101.                 
  102.       CopyBits( &(theMergeGrafPtr->portBits), &(theWindow->portBits),
  103.                 &(theMergeGrafPtr->portBits.bounds), &(theWindow->portRect),
  104.                 srcCopy, nil );
  105.    }
  106. }
  107.  
  108.  
  109. //____________________________________________________________
  110.  
  111. GrafPtr MakeNewBitMapAndSetPort( Rect *theRectPtr )
  112. {
  113.    BitMap  *theBitMapPtr;
  114.    short    theImageHeight;
  115.    short    theImageByteSize;
  116.    GrafPtr  theGrafPtr;
  117.  
  118.    theBitMapPtr = (BitMap *)NewPtr( sizeof( BitMap ) );
  119.    if ( theBitMapPtr == nil )
  120.       ExitToShell();
  121.         
  122.    theBitMapPtr->bounds = *theRectPtr;
  123.     
  124.    theBitMapPtr->rowBytes = (theRectPtr->right - theRectPtr->left + 7) / 8;
  125.    if ( ( theBitMapPtr->rowBytes % 2 ) != 0 )
  126.       theBitMapPtr->rowBytes++;
  127.       
  128.    theImageHeight = theRectPtr->bottom - theRectPtr->top;
  129.    theImageByteSize = theBitMapPtr->rowBytes * theImageHeight;
  130.    theBitMapPtr->baseAddr = NewPtr( theImageByteSize );    
  131.    if ( theBitMapPtr->baseAddr == nil )
  132.       ExitToShell();
  133.     
  134.    theGrafPtr = (GrafPtr)NewPtr( sizeof( GrafPort ) );
  135.    if ( theGrafPtr == nil )
  136.       ExitToShell();
  137.  
  138.    OpenPort( theGrafPtr );
  139.    SetPortBits( theBitMapPtr );
  140.     
  141.    return( theGrafPtr );
  142. }
  143.  
  144.  
  145. //____________________________________________________________
  146.  
  147. void  InitializeToolbox( void )
  148. {
  149.    InitGraf( &qd.thePort );
  150.    InitFonts();
  151.    InitWindows();
  152.    InitMenus();
  153.    TEInit();
  154.    InitDialogs( 0L );
  155.    FlushEvents( everyEvent, 0 );
  156.    InitCursor();
  157. }